/*************************************************************************
 * Copyright (C) [2020] by Cambricon, Inc. All rights reserved
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 *************************************************************************/

#include <chrono>
#include <condition_variable>
#include <functional>
#include <memory>
#include <string>
#include <thread>
#include <vector>

#include "glog/logging.h"

#include "easy_module.hpp"

#include "decode.hpp"

int SampleDecode::Process(std::shared_ptr<EdkFrame> frame) {
  return 0;
}

int SampleDecode::Close() {
  return 0;
}

SampleDecode::~SampleDecode() {
}

int SampleDecode::Open() {
  demuxer_ = std::unique_ptr<FFmpegDemuxer>{new FFmpegDemuxer()};
  if (demuxer_ == nullptr) {
    return -1;
  }
  int ret = demuxer_->Open(filename_.c_str());
  if (ret < 0) {
    LOG(ERROR) << "[EasyDK Sample] [Decode] Create demuxer failed";
    return -1;
  }
  fr_controller_.reset(new FrController(frame_rate_));



  fr_controller_->Start();

  return 0;
}







// class Source : public Module {
//  public:
//   Source(std::string name, int parallelism, std::string filename, int stream_id) :
//         Module(name, parallelism) {
//     filename_ = filename;
//     stream_id_ = stream_id;
//   }
//   ~Source() = default;
//   int Open() override {
//     return 0;
//   }

//   int Process(std::shared_ptr<EdkFrame> frame) override {
//     // std::cout << "source, " << __FUNCTION__ << ", "<< __LINE__ << std::endl;
//     std::shared_ptr<EdkFrame> new_frame = std::make_shared<EdkFrame>();
//     new_frame->stream_id = stream_id_;
//     new_frame->is_eos = false;

//     new_frame->frame_idx = frame_count_;
//     if (frame_count_++ == 100) {
//       new_frame->is_eos = true;
//     }
//     std::this_thread::sleep_for(std::chrono::microseconds(33));
//     if (!eos_send_) {
//       Transmit(new_frame);
//     }
//     if (new_frame->is_eos) {
//       eos_send_ = true;
//       // std::cout << frame_count_ << std::endl;
//     }
//     return 0;
//   }

//   int Close() override {
//     return 0;
//   }
//  private:
//   bool eos_send_ = false;
//   int stream_id_;
//   std::string filename_;
//   uint64_t frame_count_ = 0;
// };
